import React from 'react'

import RealTimeCodeEditor from '@/components/amis/RealTimeCodeEditor/RealTimeCodeEditor'
import { HiddenControlSchema } from 'amis/lib/renderers/Form/Hidden'
import { TextControlSchema } from 'amis/lib/renderers/Form/InputText'
import { DateTimeControlSchema } from 'amis/lib/renderers/Form/InputDate'

import { ApiObject } from 'amis-core/lib/types'
import rsqlUtils from '@/core/utils/rsql/rsqlUtils'
import coreConstants from '@/core/constants/coreConstants'
import { RsqlDataOperator } from '@/core/utils/rsql/types/RsqlDataOperator'
import { RsqlDataOperatorEnum } from '@/core/utils/rsql/types/RsqlOperatorEnum'
import defaultCrudTableUtils from '@/components/DefaultCrudTable/defaultCrudTableUtils'
import { DefaultCrudTableServerApi } from '@/components/DefaultCrudTable/types/DefaultCrudTableProps'
import ossSupplierTypeSelectUtils from '@/components/oss/ossSupplierTypeSelectUtils'
import { SelectControlSchema } from 'amis/lib/renderers/Form/Select'
import ossSupplierTypeColumnUtils from '@/components/oss/ossSupplierTypeColumnUtils'
import OssSupplierTypeEnum from '@/core/oss/enums/OssSupplierTypeEnum'
import { NumberControlSchema } from 'amis/lib/renderers/Form/InputNumber'
import ossAdminApiConfig from '@/core/oss/generated/apiconfig/ossAdminApiConfig'

const OssSupplierList = () => {
	const entityName = '对象存储供应商'
	const isModifyModeExpression = '${isEditMode}'
	const modelApiConfig = ossAdminApiConfig.ossSupplier

	const getIsTypeExpressFn = (...enumValues: string[]): string => {
		let expression = 'false'
		enumValues.forEach((value, index, array) => {
			expression = `${expression} || this.type === '${value}'`
		})
		return `(${expression})`
	}

	const isAliyunOrMinIoExpression = getIsTypeExpressFn(
		OssSupplierTypeEnum.OSS_ALIYUN.getCode(),
		OssSupplierTypeEnum.OSS_MINIO.getCode(),
	)
	const aliyunOrMinIoCommonProperties = {
		clearValueOnHidden: true,
		// disabledOn: `!${isAliyunOrMinIoExpression}`,
		// visibleOn: `${isAliyunOrMinIoExpression}`,
	}

	const aliyunFormItems = [
		{
			type: 'input-text',
			name: 'configJson.accessKey',
			label: 'AccessKey',
			placeholder: 'AccessKey',
			required: true,
			...aliyunOrMinIoCommonProperties,
		} as TextControlSchema,
		{
			type: 'input-text',
			name: 'configJson.secretKey',
			label: 'SecretKey',
			placeholder: 'SecretKey',
			required: true,
			...aliyunOrMinIoCommonProperties,
		} as TextControlSchema,
		{
			type: 'input-text',
			name: 'configJson.bucketName',
			label: 'BucketName',
			placeholder: 'BucketName',
			required: true,
			...aliyunOrMinIoCommonProperties,
		} as TextControlSchema,
		{
			type: 'input-text',
			name: 'configJson.domain',
			label: 'Domain',
			placeholder: 'Domain',
			required: true,
			...aliyunOrMinIoCommonProperties,
		} as TextControlSchema,
		{
			type: 'input-text',
			name: 'configJson.basePath',
			label: 'BasePath',
			placeholder: 'BasePath',
			required: true,
			...aliyunOrMinIoCommonProperties,
		} as TextControlSchema,
		{
			type: 'input-text',
			name: 'configJson.defaultAcl',
			label: '默认ACL',
			value: 'default',
			placeholder: '默认Access Control List',
			required: true,
			...aliyunOrMinIoCommonProperties,
		} as TextControlSchema,
		{
			type: 'input-number',
			name: 'configJson.multipartThreshold',
			label: '自动分片上传阈值-单位：B',
			value: 128 * 1024 * 1024,
			placeholder: '自动分片上传阈值，达到此大小则使用分片上传，默认 128MB',
			required: true,
			...aliyunOrMinIoCommonProperties,
		} as NumberControlSchema,
		{
			type: 'input-number',
			name: 'configJson.multipartPartSize',
			label: '自动分片上传时每个分片大小-单位：B',
			value: 32 * 1024 * 1024,
			placeholder: '自动分片上传时每个分片大小，默认 32MB',
			required: true,
			...aliyunOrMinIoCommonProperties,
		} as NumberControlSchema,
		{
			type: 'input-kv',
			name: 'configJson.attr',
			label: '自定义配置(json)',
			draggable: false,
			...aliyunOrMinIoCommonProperties,
		} as any,
	]
	let schema = defaultCrudTableUtils.getSchema({
		entityTitle: `${entityName}`,
		searchServerApi: {
			apiConfig: modelApiConfig.search,
			requestAdaptor: (api: ApiObject, context: any): ApiObject | Promise<ApiObject> => {
				const operators: RsqlDataOperator[] = [
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'id',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'type',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.LIKE_RIGHT,
						propertyName: 'title',
					} as RsqlDataOperator,
					{
						operator: RsqlDataOperatorEnum.EQUAL,
						propertyName: 'deleteFlag',
					} as RsqlDataOperator,
				]
				context[coreConstants.deleteFlagFieldName] = coreConstants.notDeleteFlag
				api = rsqlUtils.addRsqlFilter(api, context, coreConstants.api.queryFilterName, operators)
				return api
			},
		},
		addServerApi: { apiConfig: modelApiConfig.add },
		deleteServerApi: {
			apiConfig: modelApiConfig.removeLogic,
		},
		detailServerApi: {
			apiConfig: modelApiConfig.get,
			data: {
				id: '${id}',
			},
			responseData: {
				'&': '$$',
				configJson: '${config|toJson}',
			},
		},
		editServerApi: {
			apiConfig: modelApiConfig.modifyNotNullField,
			data: {
				id: '${id}',
				type: '${type}',
				title: '${title}',
				config: '${configJson|json}',
			},
		} as DefaultCrudTableServerApi,
		debug: false,
		formItems: [
			{
				type: 'hidden',
				name: 'id',
				label: 'ID',
			} as HiddenControlSchema,
			{
				...ossSupplierTypeSelectUtils.getSchema(),
				required: true,
			} as SelectControlSchema,
			{
				type: 'input-text',
				name: 'title',
				label: '名称',
				required: true,
				maxLength: 250,
			} as TextControlSchema,
			...aliyunFormItems,
			{
				type: 'input-datetime',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				static: true,
				visibleOn: isModifyModeExpression,
			} as DateTimeControlSchema,
			{
				type: 'input-datetime',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
				static: true,
				visibleOn: isModifyModeExpression,
			} as DateTimeControlSchema,
		],
		columns: [
			{
				name: 'id',
				label: 'ID',
				searchable: true,
			},
			{
				...ossSupplierTypeColumnUtils.getSchema(),
				searchable: ossSupplierTypeSelectUtils.getSchema(),
			},
			{
				name: 'title',
				label: '名称',
				searchable: true,
			},
			{
				type: 'json',
				name: 'config',
				levelExpand: 0,
				label: '配置(json)',
			},
			{
				type: 'date',
				name: 'createTime',
				label: '创建时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
			{
				type: 'date',
				name: 'updateTime',
				label: '更新时间',
				valueFormat: 'x',
				format: 'YYYY-MM-DD HH:mm:ss',
			},
		],
	})
	return <RealTimeCodeEditor code={schema} />
}

export default OssSupplierList
